<!DOCTYPE html>
<html xmlns:sa="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8">
    <title>流程实例列表</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/pear/component/pear/css/pear.css}"/>
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">流程名称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="flowName" placeholder="请输入流程名称" class="layui-input"
                               id="flowName">
                    </div>
                </div>

                <div class="layui-form-item layui-inline">
                    <button class="layui-btn layui-btn-md " lay-submit sa:hasPermission="Instance_select"
                            lay-filter="form-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="layui-btn layui-btn-md" id="resetBtn">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>

<div class="layui-card">
    <div class="layui-card-body">
        <table id="processInstanceTable" lay-filter="processInstanceTable"></table>
    </div>
</div>

<script type="text/html" id="form-toolbar">

</script>
<script type="text/html" id="processInstanceBar">
    <button class="layui-btn  layui-btn-sm" sa:hasPermission="Instance_view" lay-event="view">
        <i>查看流程</i>
    </button>
    <button class="layui-btn  layui-btn-sm" sa:hasPermission="Instance_viewData" lay-event="viewData">
        <i>查看数据</i>
    </button>
    <!--    <button class="layui-btn layui-btn-danger layui-btn-sm" lay-event="remove"><i-->
    <!--            class="layui-icon layui-icon-delete"></i>-->
    <!--    </button>-->
</script>

<script th:src="@{/pear/component/layui/layui.js}"></script>
<script th:src="@{/pear/component/pear/pear.js}"></script>
<script th:inline="javascript">
    let baseURL = /*[[@{/}]]*/ '';
    layui.use(['table'], function () {
        let table = layui.table;
        let $ = layui.jquery
        let cols = [
            [
                {field: 'id', title: 'ID', sort: true},
                {field: 'processDefinitionId', title: '流程定义ID'},
                {field: 'processDefinitionName', title: '流程名称'},
                {field: 'processDefinitionKey', title: '流程键'},
                {field: 'startTime', title: '启动时间'},
                {field: 'processDefinitionVersion', title: '流程定义版本'},
                {field: 'activityId', title: '当前节点'},
                {field: 'startActivityId', title: '启动活动的 ID'},
                {field: 'suspended', title: '是否挂起', formatter: (value) => value ? '是' : '否'},
                {fixed: 'right', title: '操作', toolbar: '#processInstanceBar', minWidth: 200}
            ]
        ];
        // 渲染表格
        table.render({
            elem: '#processInstanceTable',
            url: baseURL + 'workflow/flowable/processInstanceList',
            type: 'get',
            toolbar: '#form-toolbar',
            page: true,
            limit: 10,
            limits: [10, 20, 30],
            request: {
                pageName: 'pageNum',
                limitName: 'pageSize'
            },
            cols: cols,
            parseData: function (res) {
                return {
                    "code": res.code,
                    "msg": res.msg,
                    "count": res.data.total,
                    "data": res.data.list,
                };
            },
            response: {
                statusCode: 1000
            }
        });


        table.on('tool(processInstanceTable)', function (obj) {
            if (obj.event === 'viewData') {
                window.viewData(obj);
            } else if (obj.event === 'view') {
                window.view(obj);
            }
        });

        table.on('toolbar(processInstanceTable)', function (obj) {

        });


        window.view = function (obj) {
            let data = obj.data;
            layer.open({
                type: 1,
                title: '数据流图',
                shade: 0.1,
                shadeClose: true,
                area: ['80%', '80%'],
                content: '<img src="' + baseURL + 'workflow/flowable/process/diagram?processInstanceId=' + data.id + '" alt="流程图">',
            })
        }


        window.viewData = function (obj) {
            let data = obj.data;
            $.ajax({
                url: baseURL + "workflow/flowable/getFormDataByProcessInstanceId",
                data: {
                    processInstanceId: data.id,
                },
                dataType: 'json',
                type: 'get',
                success: function (result) {
                    if (result.code === 1000) {
                        window.taskId = obj.data.id
                        window.nowData = result.data.formData
                        window.nowData1 = result.data.formData1
                        window.nowDataList = result.data.formDataList

                        let firstFormData = JSON.parse(nowData.formData);

                        nowDataList.forEach(item => {
                            let formData = JSON.parse(item.formData);
                            Object.assign(firstFormData, formData);
                        });

                        nowData.formData = JSON.stringify(firstFormData);
                        layer.open({
                            type: 2,
                            title: '表单数据查看',
                            shade: 0.1,
                            shadeClose: true,
                            area: ['100%', '100%'],
                            content: 'form_view_check',
                        })
                    } else {
                        layer.msg(result.msg, {
                            icon: 2,
                            time: 1000
                        });
                    }
                },
                error: function () {
                    layer.msg('请求失败，请稍后再试', {
                        icon: 2,
                        time: 1000
                    });
                }
            });
        }

    });

</script>


</body>
</html>